package com.dididoctor.doctor.Activity.Main.Inquiry.NewInquiry;

import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.view.View;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.dididoctor.doctor.Activity.Main.Inquiry.InquiryDetail.InquiryDetail;
import com.dididoctor.doctor.Activity.Main.Inquiry.NewInquiry.AddSelectAnswer.AddSelectAnswerFragment;
import com.dididoctor.doctor.Activity.Main.Inquiry.NewInquiry.AddSelectAnswer.AddSelectSingAnswerFragment;
import com.dididoctor.doctor.Activity.Main.Inquiry.NewInquiry.AddSelectAnswer.AnswerBean;
import com.dididoctor.doctor.Activity.Main.Inquiry.NewInquiry.AddSingleAnswer.AddSingleAnswerFragment;
import com.dididoctor.doctor.MV.EduActivity;
import com.dididoctor.doctor.R;
import com.dididoctor.doctor.Utils.Util;

import java.util.ArrayList;
import java.util.List;

/**
 * @version 2.0
 * @类描述：
 * @项目名称：ddgh
 * @类名称： NewInquiryActivity
 * @包名称： com.dididoctor.doctor.Activity.Main.Inquiry.NewInquiry
 * @创建人： zhanjunjie
 * @创建时间：2016/4/21 19:14
 * @修改人：
 * @修改时间：2016/4/21 19:14
 * @修改备注： new parent class(用一句话描述修改了什么)
 */
public class NewInquiryActivity extends EduActivity implements NewInquiryView {

    //Fragment
    private Fragment selectSingleAnswerFragment = null;
    private Fragment selectAnswerFragment = null;
    private Fragment singleAnswerFragment = null;
    //选择按钮
    private LinearLayout mLlSelect, mLlSingle, mLlAnswer;
    private TextView mTvSelect, mTvSingle, mTvAnswer;
    private Button btn_submit;
    //请求
    private NewInquiryPresenter presenter;
    private String type = "2";//问题类型,默认是第一个选项，是单选，类型是2
    private String optype = "";//操作类型 1-新增 2-修改 3-删除
    private String qusId = "";//问题id  用于2/3
    private String diseId = "";//病种id  用于1/2
    private String content = "";//问题
    private String options = "";//问题选项


    private InquiryDetail inquiryDetail = new InquiryDetail();

    @Override
    public void init() {
        initview();
        initdata();
    }

    private void initview() {
        setText(R.id.tv_title, "添加问诊问题");
        //三个按钮
        mTvSelect = (TextView) findViewById(R.id.tv_select);
        mTvSingle = (TextView) findViewById(R.id.tv_single);
        mTvAnswer = (TextView) findViewById(R.id.tv_answer);
        mLlSelect = (LinearLayout) findViewById(R.id.ll_select);
        mLlSingle = (LinearLayout) findViewById(R.id.ll_single);
        mLlAnswer = (LinearLayout) findViewById(R.id.ll_answer);
        mLlSingle.setOnClickListener(this);
        mLlSelect.setOnClickListener(this);
        mLlAnswer.setOnClickListener(this);
    }

    private void initdata() {

        optype = getIntent().getStringExtra("optype");
        diseId = getIntent().getStringExtra("diseId");
        inquiryDetail = (InquiryDetail) getIntent().getSerializableExtra("inquiry");
        if (inquiryDetail != null) {
            optype = "2";
            qusId = inquiryDetail.getQusId();
        }
        FragmentManager fragmentManager = getSupportFragmentManager();
        FragmentTransaction transaction = fragmentManager.beginTransaction();
        //存值
        Bundle bundle = new Bundle();
        bundle.putSerializable("inquiry", inquiryDetail);
        //Fragment初始化
        selectSingleAnswerFragment = new AddSelectSingAnswerFragment(this);
        selectAnswerFragment = new AddSelectAnswerFragment(this);
        singleAnswerFragment = new AddSingleAnswerFragment();
        //传值
        selectAnswerFragment.setArguments(bundle);
        selectSingleAnswerFragment.setArguments(bundle);
        singleAnswerFragment.setArguments(bundle);
        //将Fragment添加到FragmentTransaction
        transaction.add(R.id.fl_answer, singleAnswerFragment);
        transaction.add(R.id.fl_answer, selectAnswerFragment);
        transaction.add(R.id.fl_answer, selectSingleAnswerFragment);
        if (inquiryDetail != null) {
            clean(transaction);
            if ("1".equals(inquiryDetail.getType())) {
                mTvAnswer.setTextColor(getResources().getColor(R.color.white));
                mLlAnswer.setBackground(getResources().getDrawable(R.drawable.bg_doctor_label_yellow));
                transaction.show(singleAnswerFragment);
                type = "1";
            } else if ("2".equals(inquiryDetail.getType())) {
                mTvSingle.setTextColor(getResources().getColor(R.color.white));
                mLlSingle.setBackground(getResources().getDrawable(R.drawable.bg_doctor_label_yellow));
                transaction.show(selectSingleAnswerFragment);
                type = "2";
            } else {
                mTvSelect.setTextColor(getResources().getColor(R.color.white));
                mLlSelect.setBackground(getResources().getDrawable(R.drawable.bg_doctor_label_yellow));
                transaction.show(selectAnswerFragment);
                type = "3";
            }
        }
        transaction.commit();
        presenter = new NewInquiryPresenter(this, this);

        btn_submit = (Button) findViewById(R.id.btn_submit);
        btn_submit.setOnClickListener(new View.OnClickListener() {
                                          @Override
                                          public void onClick(View v) {
                                              submitcancel();
                                              if ("1".equals(type)) {//问答
                                                  content = ((AddSingleAnswerFragment) singleAnswerFragment).getContent();
                                                  if (Util.isEmpty(content)) {
                                                      showToastMessage("请输入问题");
                                                      submitok();
                                                  } else {
                                                      //optype从上一页面获取
                                                      //qusId从上以页面获取(修改才会用到)
                                                      //diseId从上一页面获取
                                                      //options问题选择，单选和多选用到
                                                      //content问题
                                                      presenter.qusoperate(optype, qusId, diseId, type, options, content);
                                                  }
                                              } else if ("2".equals(type)) { //单选
                                                  content = ((AddSelectSingAnswerFragment) selectSingleAnswerFragment).answercontent();
                                                  if (Util.isEmpty(content)) {
                                                      showToastMessage("请输入问题");
                                                      submitok();
                                                      return;
                                                  }
                                                  List<AnswerBean> answerBeans = new ArrayList<AnswerBean>();
                                                  answerBeans = ((AddSelectSingAnswerFragment) selectSingleAnswerFragment).answerlist();
                                                  if (answerBeans.size() < 1) {
                                                      showToastMessage("请输入选项");
                                                      submitok();
                                                  } else {
                                                      for (int i = 0; i < answerBeans.size(); i++) {
                                                          if (Util.isEmpty(answerBeans.get(i).getOpction())) {
                                                              showToastMessage("请填写完整选项内容");
                                                              submitok();
                                                              return;
                                                          }
                                                      }
                                                      StringBuffer buffer = new StringBuffer();
                                                      for (int i = 0; i < answerBeans.size(); i++) {
                                                          buffer.append(answerBeans.get(i).getOpction() + ",|,");
                                                      }
                                                      options = buffer.toString();
                                                      presenter.qusoperate(optype, qusId, diseId, type, options, content);


                                                  }
                                              } else {//多选
                                                  content = ((AddSelectAnswerFragment) selectAnswerFragment).answercontent();
                                                  if (Util.isEmpty(content)) {
                                                      showToastMessage("请输入问题");
                                                      submitok();
                                                      return;
                                                  }
                                                  List<AnswerBean> answerBeans = new ArrayList<AnswerBean>();
                                                  answerBeans = ((AddSelectAnswerFragment) selectAnswerFragment).answerlist();
                                                  if (answerBeans.size() < 1) {
                                                      showToastMessage("请输入选项");
                                                      submitok();
                                                  } else {
                                                      for (int i = 0; i < answerBeans.size(); i++) {
                                                          if (Util.isEmpty(answerBeans.get(i).getOpction())) {
                                                              showToastMessage("请填写完整选项内容");
                                                              submitok();
                                                              return;
                                                          }
                                                      }
                                                      StringBuffer buffer = new StringBuffer();
                                                      for (int i = 0; i < answerBeans.size(); i++) {
                                                          buffer.append(answerBeans.get(i).getOpction() + ",|,");
                                                      }
                                                      options = buffer.toString();
                                                      presenter.qusoperate(optype, qusId, diseId, type, options, content);
                                                  }

                                              }
                                          }
                                      }

        );


    }

    private void submitcancel() {
        btn_submit.setEnabled(false);
        btn_submit.setFocusable(false);
        btn_submit.setBackground(getResources().getDrawable(R.drawable.btn_code_normal));
    }

    private void submitok() {
        btn_submit.setEnabled(true);
        btn_submit.setFocusable(true);
        btn_submit.setBackground(getResources().getDrawable(R.drawable.btn_home_selector));
    }

    @Override
    public void onClickEvent(View view) {
        FragmentTransaction transaction = getSupportFragmentManager().beginTransaction();
        clean(transaction);
        switch (view.getId()) {
            case R.id.ll_single:
                mTvSingle.setTextColor(getResources().getColor(R.color.white));
                mLlSingle.setBackground(getResources().getDrawable(R.drawable.bg_doctor_label_yellow));
                transaction.show(selectSingleAnswerFragment);
                type = "2";
                break;
            case R.id.ll_select:
                mTvSelect.setTextColor(getResources().getColor(R.color.white));
                mLlSelect.setBackground(getResources().getDrawable(R.drawable.bg_doctor_label_yellow));
                transaction.show(selectAnswerFragment);
                type = "3";
                break;
            case R.id.ll_answer:
                mTvAnswer.setTextColor(getResources().getColor(R.color.white));
                mLlAnswer.setBackground(getResources().getDrawable(R.drawable.bg_doctor_label_yellow));
                transaction.show(singleAnswerFragment);
                type = "1";
                break;
        }
        transaction.commit();
    }

    private void clean(FragmentTransaction transaction) {
        transaction.hide(selectSingleAnswerFragment);
        transaction.hide(selectAnswerFragment);
        transaction.hide(singleAnswerFragment);
        mTvSelect.setTextColor(getResources().getColor(R.color.main_color));
        mTvSingle.setTextColor(getResources().getColor(R.color.main_color));
        mTvAnswer.setTextColor(getResources().getColor(R.color.main_color));
        mLlSelect.setBackground(getResources().getDrawable(R.drawable.bg_doctor_label_yellow_with));
        mLlSingle.setBackground(getResources().getDrawable(R.drawable.bg_doctor_label_yellow_with));
        mLlAnswer.setBackground(getResources().getDrawable(R.drawable.bg_doctor_label_yellow_with));
    }

    @Override
    public void setContentLayout() {
        setContentView(R.layout.activity_newinquiry);
    }

    @Override
    public void InsterOrUpdateAnswer(String qusId) {
        setResult(RESULT_OK);
        finish();
    }

    @Override
    public void InsterOrUpdateAnswerFail() {
        submitok();
        showToastMessage("请求失败");
    }
}
